package entity

import (
	"fmt"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

// Zywhrdr 掌银维护人导入
type Zywhrdr struct {
	ID    int    `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	CUNO  string `gorm:"column:CUNO"`
	CUNM  string `gorm:"column:CUNM"`
	EMNO2 string `gorm:"column:EMNO2"`
	EMNM2 string `gorm:"column:EMNM2"`
	EMID  int    `gorm:"column:EMID"`
	CUID  int    `gorm:"column:CUID"`
}

// TableName
func (Zywhrdr) TableName() string {
	return "T_INDI_CLIE_WHR"
}

// BeforeSave
func (u *Zywhrdr) BeforeSave(tx *gorm.DB) (err error) {
	var emid *int
	tx.Raw(`select "ID" from "T_SysUser" where "SysUserName" = ?`, u.EMNM2).Row().Scan(&emid)
	if emid != nil {
		var cuid, emid *int
		sqlEx := `SELECT CUID  from T_INDI_CLIE_ZY where CUNO = ?`
		base.CHDB.Raw(sqlEx, u.CUNO).Row().Scan(&cuid)
		err = tx.Raw(`select "ID"  from "T_SysUser" where "SysUserID" = ?`, u.EMNO2).Row().Scan(&emid)
		if err != nil {
			return
		}
		u.CUID = *cuid
		u.EMID = *emid
	} else {
		err = fmt.Errorf("员工%s在系统中不存在", u.EMNM2)
		return
	}
	return
}

func ZywhrdrAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	base.CHDB.Exec("system reload dictionary pas_olap_nj.DICT_INDI_CLIE_WHR")
	return
}

// ShCompClieQygmEF 返回结构体
func ZywhrdrEF() base.EntityFunc {
	return func() interface{} {
		entity := Zywhrdr{}
		return &entity
	}
}

// ShCompClieQygmESF 返回结构体数组
func ZywhrdrESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []Zywhrdr
		return &entities
	}
}
func init() {
	base.SetAfterCallBack("Zywhrdr", ZywhrdrAfterImport)
	base.DaoPoolObj.Put("Zywhrdr", base.NewDefaultDao(ZywhrdrEF(), ZywhrdrESF(), nil, "my"))
}
